const BaseController = require('./base');

class ExamController extends BaseController {
  async addTestPaper() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      content: { type: 'content' },
    });
    const testPaperInfo = ctx.request.body;
    const res = await ctx.service.exam.addTestPaper(testPaperInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async updateTestPaper() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
      content: { type: 'content' },
    });
    const testPaperInfo = ctx.request.body;
    const res = await ctx.service.exam.updateTestPaper(testPaperInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async delTestPaper() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const res = await ctx.service.exam.delTestPaper(ids);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async getTestPaper() {
    const { ctx } = this;
    const { id, pageNum, pageSize, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.exam.getTestPaper(id);
      if (res) {
        this.success(res);
      } else {
        this.fail('试卷不存在');
      }
    } else {
      const res = await ctx.service.exam.getTestPapers(Number(pageNum), Number(pageSize), name);
      if (res) {
        this.success(res);
      } else {
        this.fail('查询失败');
      }
    }
  }


  async addTest() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      begin: { type: 'string' },
      end: { type: 'string' },
      is_limit_jump: { type: 'boolean' },
      limit_jump_times: { type: 'number' },
      face_recording: { type: 'boolean' },
      screen_recording: { type: 'boolean' },
    });
    const testInfo = ctx.request.body;
    const result = await ctx.service.exam.addTest(testInfo);
    if (result) {
      return this.success('添加成功');
    }
    return this.fail('添加失败');

  }

}

module.exports = ExamController;
